package spring.cloud.eurekaclientuser.controller;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.cloud.vo.ResultMessage;


@RestController
@RequestMapping("/hystrix")
public class HystrixController {

    private static Long MAX_SLEEP_TIME = 5000L;

    @GetMapping("/timeout")
    public ResultMessage timeout(){
        Long sleepTime = (long) (MAX_SLEEP_TIME * Math.random());
        try{
            Thread.sleep(sleepTime);
        }catch (Exception ex){
            System.out.println("执行异常");
        }
        return new ResultMessage(true,"执行时间"+sleepTime);
    }

    @GetMapping(path = "/exp/{msg}",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMessage exp(@PathVariable("msg") String msg){
        if("spring".equals(msg)){
            return new ResultMessage(true,msg);
        }else{
            throw new RuntimeException("出现了异常，请检查参数msg是否为spring");
        }
    }
}
